# -*- coding: utf-8 -*-
import json
import os

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QDockWidget, QAction, QFileDialog, QHBoxLayout, QComboBox, QRadioButton, QLabel, \
    QVBoxLayout, QGridLayout, QLineEdit
import cv2
import copy
import numpy as np

from imageManager import img_mng

PARA_PATH = os.path.dirname(__file__)


class drawGui(QtWidgets.QWidget):
    add_func_signal = QtCore.pyqtSignal(int, dict)
    para = dict()
    index = 0

    def __init__(self, parent=None):
        super(drawGui, self).__init__(parent)
        self.imgMat = None
        self.start_point: tuple = (0, 0)
        self.end_point: tuple = (0, 0)

    def setPara(self, func_id, para):
        self.index = func_id
        if para is not None:
            self.para = para

    def setStartPoint(self, point):
        self.start_point = point

    def setEndPoint(self, point):
        self.end_point = point

    def setImg(self, img):
        self.imgMat = copy.deepcopy(img)

    def initPara(self):
        parafile = os.path.join(PARA_PATH, "module_para.json")
        with open(parafile, "r", encoding='utf-8') as f:
            self.para = json.loads(f.read())

    def initUI(self):
        self.initPara()
        btn_ok = QtWidgets.QPushButton(self)
        btn_ok.setText("OK")
        btn_ok.clicked.connect(self.btn_ok_clicked)
        btn_cancel = QtWidgets.QPushButton(self)
        btn_cancel.setText("CANCEL")
        btn_cancel.clicked.connect(self.btn_cancel_clicked)
        layout1 = QHBoxLayout()
        layout1.addWidget(btn_ok)
        layout1.addWidget(btn_cancel)

        lb_type = QLabel("Type:")
        cb_type = QComboBox(self)
        cb_type.addItem("line")
        cb_type.addItem("circle")
        cb_type.addItem("rectangle")
        cb_type.addItem("ellipse")
        cb_type.addItem("polylines")
        cb_type.currentIndexChanged[str].connect(self.type_change)

        layout2 = QHBoxLayout()
        layout2.addWidget(lb_type)
        layout2.addWidget(cb_type)

        self.lb_pos0 = QLabel("Start Position:")
        self.lb_pos1 = QLabel("End Position:")
        self.le_pos0 = QLineEdit(self)
        self.le_pos1 = QLineEdit(self)
        grid_layout = QGridLayout()
        grid_layout.addWidget(self.lb_pos0, 0, 0)
        grid_layout.addWidget(self.le_pos0, 0, 1)
        grid_layout.addWidget(self.lb_pos1, 1, 0)
        grid_layout.addWidget(self.le_pos1, 1, 1)

        v_layout = QVBoxLayout()
        v_layout.addLayout(layout2)
        v_layout.addLayout(grid_layout)
        v_layout.addStretch(1)
        v_layout.addLayout(layout1)
        # self.btn1.setChecked(True)

        self.setLayout(v_layout)

    def type_change(self, draw_type):
        if draw_type == "line":
            self.lb_pos0.setText("start:")
            self.lb_pos1.setText("end:")
        elif draw_type == "circle":
            self.lb_pos0.setText("center:")
            self.lb_pos1.setText("radius:")
        elif draw_type == "rectangle":
            self.lb_pos0.setText("left top:")
            self.lb_pos1.setText("right bottom:")

    def draw_line(self):
        pass

    def btn_ok_clicked(self):
        if self.imgMat is None:
            return
        font = cv2.FONT_ITALIC
        cv2.putText(self.imgMat, "hello", (10, 40), font, 1, (255, 255, 255), 2)

        img_mng.dispImg(self.imgMat)

        with open("module_para.json", "w", encoding='utf-8') as f:
            f.write(json.dumps(self.para, indent=4))

    def btn_cancel_clicked(self):
        self.run()
        self.close()

    def run(self, para):
        if para["draw"] == "line":
            x0 = int(para["position"]["x0"])
            y0 = int(para["position"]["y0"])
            x1 = int(para["position"]["x1"])
            y1 = int(para["position"]["y1"])
            color = int(para["color"])
            img = np.zeros((512, 512, 3), np.uint8)
            cv2.line(img, (x0, y0), (x1, y1), (255, 0, 0), 3)
            cv2.imshow("img", img)
            cv2.waitKey(0)
            img_mng.img = img
            img_mng.dispImg(img)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    ui = drawGui()
    ui.initUI()
    ui.show()
    sys.exit(app.exec_())
